package com.example.dormitory_manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dormitory_manager.entity.Dormitory;
import com.example.dormitory_manager.entity.Student;
import com.example.dormitory_manager.form.SearchForm;
import com.example.dormitory_manager.form.StudentForm;
import com.example.dormitory_manager.mapper.DormitoryMapper;
import com.example.dormitory_manager.mapper.StudentMapper;
import com.example.dormitory_manager.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dormitory_manager.utils.CommonUtil;
import com.example.dormitory_manager.vo.PageVo;
import com.example.dormitory_manager.vo.StudentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.pattern.PathPattern;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-10-02
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private DormitoryMapper dormitoryMapper;

    @Override
    public Boolean saveStudent(Student student) {

        //添加学生数据
        student.setCreateDate(CommonUtil.createDate());
        int insert = this.studentMapper.insert(student);
        if (insert != 1) return false;

        //修改宿舍数据 当available==0时 不能在住人 返回false
        Dormitory dormitory = this.dormitoryMapper.selectById(student.getDormitoryId());
        if (dormitory.getAvailable() == 0) {
            return false;
        }
        dormitory.setAvailable(dormitory.getAvailable() - 1);
        int update = this.dormitoryMapper.updateById(dormitory);
        if (update != 1) return false;
        return true;
    }

    @Override
    public PageVo list(Integer page, Integer size) {
        Page<Student> studentPage =new Page<>(page,size);
        Page<Student> resultPage =this.studentMapper.selectPage(studentPage,null);
        List<Student> studentList=resultPage.getRecords();

        //vo装换
        List<StudentVo> studentVosList=new ArrayList<>();
        for (Student student : studentList) {
            StudentVo studentVo=new StudentVo();
            //给对象复制值到另一个对象上
            BeanUtils.copyProperties(student,studentVo);
            Dormitory dormitory=this.dormitoryMapper.selectById(student.getDormitoryId());
            studentVo.setDormitoryName(dormitory.getName());
            studentVosList.add(studentVo);
        }

        PageVo pageVo=new PageVo();
        pageVo.setData(studentVosList);
        pageVo.setTotal(resultPage.getTotal());
        return pageVo;
    }

    @Override
    public PageVo search(SearchForm searchForm) {
        Page<Student> studentPage =new Page<>(searchForm.getPage(),searchForm.getSize());
        Page<Student> resultPage =null;
        if(searchForm.getValue().equals("")){
            resultPage = this.studentMapper.selectPage(studentPage,null);
        }else{
            QueryWrapper<Student> queryWrapper=new QueryWrapper<>();
            queryWrapper.like(searchForm.getKey(),searchForm.getValue());
            resultPage=this.studentMapper.selectPage(studentPage,queryWrapper);
        }

        List<Student> studentList=resultPage.getRecords();

        //vo装换
        List<StudentVo> studentVosList=new ArrayList<>();
        for (Student student : studentList) {
            StudentVo studentVo=new StudentVo();
            //给对象复制值到另一个对象上
            BeanUtils.copyProperties(student,studentVo);
            Dormitory dormitory=this.dormitoryMapper.selectById(student.getDormitoryId());
            studentVo.setDormitoryName(dormitory.getName());
            studentVosList.add(studentVo);
        }

        PageVo pageVo=new PageVo();
        pageVo.setData(studentVosList);
        pageVo.setTotal(resultPage.getTotal());
        return pageVo;
    }

    @Override
    public boolean update(StudentForm studentForm) {
        Student student=new Student();
        BeanUtils.copyProperties(studentForm,student);
        //更新宿舍数据
        int update=this.studentMapper.updateById(student);
        if(update!=1) return false;
        //判断有没有换宿舍 如果换宿舍 之前住的宿舍 availible +1  现在住的宿舍availible - 1
        if(!student.getDormitoryId().equals(studentForm.getOldDormitoryId())){
            try {
                this.dormitoryMapper.addAvailible(studentForm.getOldDormitoryId());
                this.dormitoryMapper.subAvailible(studentForm.getDormitoryId());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return true;
    }

    @Override
    public boolean deleteById(Integer id) {
        //获取学生详情
        Student student=this.studentMapper.selectById(id);
        //修改宿舍数据
        try {
            Dormitory dormitory=this.dormitoryMapper.selectById(student.getDormitoryId());
            //判断是否超过最大入住量
            if(dormitory.getType()>dormitory.getAvailable()){
                this.dormitoryMapper.addAvailible(student.getDormitoryId());
            }
        } catch (Exception e) {
            return false;
        }

        //删除学生数据
        int delete=this.studentMapper.deleteById(id);
        if(delete!=1)return  false;


        return true;
    }
}
